package com.ruoyi.business.listener;


import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.wecom.client.WeComUtils;
import com.ruoyi.common.wecom.constants.WeComUrlConstants;
import com.ruoyi.workflow.event.CopyUserEvent;
import com.ruoyi.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.common.engine.api.delegate.event.FlowableEventType;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.Collection;

/**
 * Flowable 全局监听器
 * 流程结束时
 *
 * @author zyb
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class CopyUserListener implements FlowableEventListener {

    private final WeComUtils weComUtils;
    private final ISysUserService userService;

    @Override
    public Collection<? extends FlowableEventType> getTypes() {
        return null;
    }

    @Override
    public void onEvent(FlowableEvent flowableEvent) {

    }

    @EventListener
    public void onCopyUserEvent(CopyUserEvent event) {
        log.info("抄送监听器，监听成功");
        if (CollUtil.isNotEmpty(event.copyList)) {
            event.copyList.forEach(v->{
                weComUtils.sendTextMessage(userService.selectUserById(v.getUserId()).getUserName(),
                    WeComUrlConstants.TITLE_4,
                    v.getProcessName() + ";项目：",
                    v.getOriginatorName());
            });
        }
    }

    @Override
    public boolean isFailOnException() {
        return false;
    }

    @Override
    public boolean isFireOnTransactionLifecycleEvent() {
        return false;
    }

    @Override
    public String getOnTransaction() {
        return null;
    }


}
